package com.eco.fanliapp.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.constant.ApiEnum;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.SyTaskType;
import com.eco.fanliapp.entity.SyUserTask;
import com.eco.fanliapp.service.*;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-08-07
 */
@RestController
@RequestMapping("/app/task")
public class SyUserTaskController extends BaseController {
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private ISyUserTaskService iSyUserTaskService;
    @Autowired
    private ISyUserMoneyService iSyUserMoneyService;
    @Autowired
    private IAppUserService iAppUserService;
    @Autowired
    private UpdateUserCommon updateUserCommon;
    @Autowired
    private ISyTaskTypeService iSyTaskTypeService;
    @Autowired
    private RedisTemplateService redisTemplateService;


    /**
     * 阴影任务点击
     * @return
     */
    @GetMapping("/popup/click")
    @Synchronized
    public ServerResponse popupClick() {
        AppUser u = getAppUser();
        if (u == null || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301,"用户不存在");
        }
        AppUser appUser = appUserService.selectAppUserByUserId(u.getUserId());
        if (appUser == null || appUser.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301,"用户不存在");
        }else{
            QueryWrapper<SyUserTask> syUserTaskQueryWrapper = new QueryWrapper<>();
            syUserTaskQueryWrapper.eq("user_id", appUser.getUserId());
            syUserTaskQueryWrapper.eq("task_type_id", 1);
            syUserTaskQueryWrapper.eq("enable", 1);
            syUserTaskQueryWrapper.eq("deleted", 0);
            List<SyUserTask> lst = iSyUserTaskService.list(syUserTaskQueryWrapper);

            if(lst == null || lst.size()<=0) {
                SyUserTask syUserTask = new SyUserTask();
                syUserTask.setTaskStatus(1);
                syUserTask.setUserId(appUser.getUserId());
                syUserTask.setTaskTypeId(1);
                syUserTask.setTaskType(1);
                syUserTask.setTaskRewardType(1);
                syUserTask.setTaskRewardNum(0);
                syUserTask.setTaskRewardDate(new Date());
                syUserTask.setEnable(1);
                syUserTask.setDeleted(0);
                syUserTask.setTaskRewardMoney(new BigDecimal(0.36));
                int i = iSyUserTaskService.insertsyUserTask(syUserTask);
                if (i>0) {
                    return ServerResponse.createBySuccess("成功");
                }
            }
        }
        return ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 获取新人任务复制链接弹窗-新
     * @return
     */
    @GetMapping(value = "/redPacketPopUp")
    public ServerResponse redPacketPopUp() {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserTaskService.redPacketPopUp(u.getUserId()) ;
    }

    /**
     * 获取首购任务弹窗-新
     * @return
     */
    @GetMapping(value = "/taskPacketPopUp")
    public ServerResponse taskPacketPopUp() {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserTaskService.taskPacketPopUp(u.getUserId()) ;
    }


    /**
     * 查询签到状态，是否签到
     * @return
     */
    @GetMapping("/sign/getSignStatus")
    @ResponseBody
    public ServerResponse getSignStatus() {
        AppUser u = getAppUser();
        if (u == null || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301,"用户不存在");
        }
        AppUser appUser = appUserService.selectAppUserByUserId(u.getUserId());
        if (appUser == null || appUser.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301,"用户不存在");
        }else{
            return iSyUserTaskService.getSignStatus(appUser.getUserId());
        }
    }

    /**
     * 分享商品到微信群领金币 自动兑换 (添加账户变更记录)
     * @return
     */
    @GetMapping("/share/toWxMoney")
    @Synchronized
    public ServerResponse shareToWxMoney() {
        String remark = "分享商品到微信群领金币 自动兑换";
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_7";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(7);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }
        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),7,syTaskType.getTaskRewardNum(),2,null,remark);
        if (exchangeMoney !=null){

            map.put("title","分享微信群奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime", ApiEnum.DIS_TIME);//弹窗显示时间毫秒
            return ServerResponse.createBySuccess("分享成功",map);
        }
        return ServerResponse.createByErrorMessage("分享失败");
    }

    /**
     * 好评领金币 自动兑换 (添加账户变更记录)
     * @return
     */
    @GetMapping("/comments/toShopMoney")
    @Synchronized
    public ServerResponse commentsToShopMoney() {
        String remark = "好评领金币 自动兑换";
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_9";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(9);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }

        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),9,syTaskType.getTaskRewardNum(),2,null,remark);
        if (exchangeMoney !=null){

            map.put("title","好评奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime",ApiEnum.DIS_TIME);//弹窗显示时间2秒
            return ServerResponse.createBySuccess("评论成功",map);
        }
        return ServerResponse.createByErrorMessage("评论失败");
    }


    /**
     * 每日推送领金币 自动兑换 (添加账户变更记录)
     * @return
     */
    @GetMapping("/message/pushViewMoney")
    @Synchronized
    public ServerResponse messagePushViewMoney() {
        String remark = "每日推送领金币 自动兑换";

        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_10";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(10);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }

        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),10,syTaskType.getTaskRewardNum(),2,null,remark);
        if (exchangeMoney !=null){
            map.put("title","查看推送奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime",ApiEnum.DIS_TIME);//弹窗显示时间2秒
            return ServerResponse.createBySuccess("推送成功",map);
        }
        return ServerResponse.createByErrorMessage("推送失败");
    }

    /**
     * 新手任务，复制链接--新(添加账户变更记录)
     * @return
     */
    @GetMapping("/goods/copyLink")
    @Synchronized
    public ServerResponse getNewRedMoneyPacket() {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserTaskService.goodsCopyLink(u.getUserId());
    }

    /**
     * 新手任务，完善手机号--新(添加账户变更记录)
     * @return
     */
    @PostMapping("/perfect/pohoneNumber")
    @Synchronized
    public ServerResponse  perfectPohoneNumber(String phoneNumber, String code) {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        AppUser user = iAppUserService.selectAppUserByAlipayPhoneNumber(phoneNumber);
        if (user != null) {
            return ServerResponse.createByErrorMessage("手机号已绑定，请更换手机号");
        }
        return iSyUserTaskService.perfectPohoneNumber(u.getUserId(),phoneNumber,code);
    }


    /**
     * 日常任务7天签到(添加账户变更记录)
     * @return
     */
    @GetMapping("/sign/toSignTask")
    @ResponseBody
    public ServerResponse toSignTask() {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserMoneyService.userSignTaskSuccessUpdateGold(u.getUserId());
    }

    /**
     * 加入反馈群 自动兑换 (添加账户变更记录)
     * @return
     */
    @GetMapping("/feedback/joinMoney")
    @Synchronized
    public ServerResponse feedbackJoinMoney() {
        String remark = "加入反馈群，自动兑换现金";

        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_12";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(12);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }
        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),12,syTaskType.getTaskRewardNum(),3,null,remark);
        if (exchangeMoney !=null){
            map.put("title","加入用户反馈群奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime",ApiEnum.DIS_TIME);//弹窗显示时间2秒
            return ServerResponse.createBySuccess("奖励到账",map);
        }
        return ServerResponse.createByErrorMessage("任务已完成");
    }

    /**
     * 问卷填写 自动兑换(添加账户变更记录)
     * @return
     */
    @GetMapping("/question/writeMoney")
    @Synchronized
    public ServerResponse questionWriteMoney() {
        String remark = "问卷填写，自动兑换";
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_13";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(13);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }
        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),13,syTaskType.getTaskRewardNum(),3,null,remark);
        if (exchangeMoney !=null){
            map.put("title","问卷填写奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime",ApiEnum.DIS_TIME);//弹窗显示时间2秒
            return ServerResponse.createBySuccess("奖励到账",map);
        }
        return ServerResponse.createByErrorMessage("任务已完成");
    }

    /**
     * 观看视频 自动兑换(添加账户变更记录)
     * @return
     */
    @GetMapping("/video/watchMoney")
    @Synchronized
    public ServerResponse videoWatchMoney() {
        String remark = "观看视频，自动兑换";

        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_14";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(14);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }
        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),14,syTaskType.getTaskRewardNum(),3,null,remark);
        if (exchangeMoney !=null){
            map.put("title","观看视频奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime",ApiEnum.DIS_TIME);//弹窗显示时间2秒
            return ServerResponse.createBySuccess("奖励到账",map);
        }
        return ServerResponse.createByErrorMessage("任务已完成");
    }

    /**
     * 去会场 自动兑换(添加账户变更记录)
     * @return
     */
    @GetMapping("/home/goToCatMoney")
    @Synchronized
    public ServerResponse goToCatMoney() {
        String remark = "逛天猫会场，自动兑换";

        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        HashMap map = new HashMap();
        SyTaskType syTaskType = null;
        //查询金币任务
        String strKey = "app_user_tasktype_setup_15";
        if(redisTemplateService.hasKey(strKey)){
            syTaskType = redisTemplateService.getTByKey(strKey,SyTaskType.class);
        }
        if(syTaskType == null){
            syTaskType = iSyTaskTypeService.selectSyTaskTypeById(15);
            redisTemplateService.set(strKey,syTaskType,60*60);
        }
        BigDecimal exchangeMoney = updateUserCommon.updateUserGoldExchangeMoney( u.getUserId(),15,syTaskType.getTaskRewardNum(),3,null,remark);
        if (exchangeMoney !=null){
            map.put("title","观看视频奖励");
            map.put("goldNum",syTaskType.getTaskRewardNum());
            map.put("exchangeMoney",exchangeMoney.toString());
            map.put("displayTime",ApiEnum.DIS_TIME);//弹窗显示时间2秒
            return ServerResponse.createBySuccess("奖励到账",map);
        }
        return ServerResponse.createByErrorMessage("任务已完成");
    }


    /**
     * 是否开启签到提醒
     * @return
     */
    @GetMapping("/sign/signOnOffRemind")
    @ResponseBody
    public ServerResponse signOnOffRemind(String signPush) {
        AppUser u = getAppUser();
        if (u == null || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301,"用户不存在");
        }
        AppUser appUser = appUserService.selectAppUserByUserId(u.getUserId());
        if (appUser == null || appUser.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301,"用户不存在");
        }else{
            return iSyUserTaskService.signRemind(appUser.getUserId(),signPush);
        }
    }
}
